package com.leanote.berial.tingphone.parsers;

import android.util.Log;
import com.leanote.berial.tingphone.bean.CategoryTagMenu;
import com.leanote.berial.tingphone.bean.DiscoverCategory;
import com.leanote.berial.tingphone.bean.DiscoverTab;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with InetlliJ IDEA.
 * Project: com.liuhui.zhufengfm.app.parsers
 * user  Pinocchio
 * Date 2015/7/28
 * Email:liu594545591@126.com
 */
public final class DataParser {
    private static final String TAG = "DataParser";

    private DataParser() {

    }

    /**
     * 解析发现的分类
     * @param json
     * @return
     */
    public static List<DiscoverCategory> parseDiscoverCategory(JSONObject json){
        List<DiscoverCategory> categories =null;
        if (json != null) {
            try {
                int code = json.getInt("ret");
                if (code == 0){
                    JSONArray list = json.getJSONArray("list");
                    if (list.length()>0) {
                        categories =  new ArrayList();
                        for (int i = 0; i < list.length(); i++) {
                            JSONObject jsonObject = list.getJSONObject(i);
                            DiscoverCategory category = new DiscoverCategory();
                            category.parseJSON(jsonObject);
                            categories.add(category);
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return categories;
    }
    /**
     * 解析CategoryTagMenuTask 返回的JSON结果
     * @param json JSONObject
     * @return List&lt;CategoryTagMenu&gt;
     */
    public static List<CategoryTagMenu> parseCategoryMenu(JSONObject json){
        List<CategoryTagMenu> ret = null;
        if (json!=null){
            try {
                int code = json.getInt("ret");
                if (code == 0) {
                    JSONObject data = json.getJSONObject("data");
                    int  category_count = data.getInt("category_count");
                    if (category_count>0){
                        JSONArray array = data.getJSONArray("category_list");
                        category_count = array.length();
                        if (category_count>0){
                            ret = new LinkedList();
                            for (int i = 0; i < category_count; i++) {
                                JSONObject jsonObject = array.getJSONObject(i);
                                CategoryTagMenu menu = new CategoryTagMenu();
                                menu.parseJSON(jsonObject);
                                ret.add(menu);
                                Log.d(TAG,menu.toString());
                            }
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    /**
     * 解析发现的Tab标题
     * @param jsonObject
     * @return
     */
    public static List<DiscoverTab> parseDiscoverTabs(JSONObject jsonObject) {
        List<DiscoverTab> listTabs = new ArrayList<DiscoverTab>();
        int code;
        try {
            code = jsonObject.getInt("ret");
            if (code == 0) {
                JSONObject tabs = jsonObject.getJSONObject("tabs");
                int count = tabs.optInt("count");
                if (count == 5) {
                    JSONArray list = tabs.getJSONArray("list");
                    int len = list.length();
                    if (len > 0) {
                        listTabs.clear();
                        for (int i = 0; i < len; i++) {
                            DiscoverTab tab = new DiscoverTab();
                            tab.parseJSON(list.getJSONObject(i));
                            listTabs.add(tab);
                        }

                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return listTabs;
    }
}
